package resource;

import java.awt.Point;
import java.io.FileReader;
import java.util.ArrayList;

import javax.xml.parsers.SAXParserFactory;

import launch.GamerControls;
import launch.SimpleTest;

import character.Hero;
import org.newdawn.slick.util.pathfinding.Path;
import org.xml.sax.InputSource;

import resource.handler.GameCharacterHandler;

/**
 * Gestionnaire de personnage
 * @author Kent
 *
 */
public class GameCharacterManager {
	ArrayList<Hero> l_gc;
	
	/**
	 * Constructeur de gestionnaire de personnage
	 * @param file
	 */
	public GameCharacterManager(String file){
		l_gc = new ArrayList<Hero>();
	    javax.xml.parsers.SAXParserFactory spf = SAXParserFactory.newInstance();
	    spf.setValidating(false);
	    javax.xml.parsers.SAXParser sp;
		try {
			sp = spf.newSAXParser();
		    org.xml.sax.InputSource input = new InputSource(new FileReader(file));
		    GameCharacterHandler handler = new GameCharacterHandler(l_gc);
		    sp.parse(input, handler);
		} catch (Exception e) {
			e.printStackTrace();
		};
	}
	
	/**
	 * Renvoie un perso. selon sa position
	 * @param index : sa position
	 * @return GameCharacter | null si introuvable
	 */
	public Hero getGameCharacter(int index){
		return l_gc.get(index);
	}
	
	/**
	 * Renvoie un perso. selon son nom (UNIQUE)
	 * @param name : nom du personnage
	 * @return GameCharacter | null si introuvable
	 */
	public Hero getGameCharacter(String name){
		for(Hero g : l_gc){
			if(g.name.equals(name)){
				return g;
			}
		}
		return null;
	}
	
	public void ChangeMap(){
		String name = SimpleTest.getMap().getName();
		for(Hero her : l_gc){
			Point p = her.getPosition(name);
			if(p.x != -1){
				her.chemin = new Path();
				GamerControls.Teleport("DOWN",her, her.getPosition(name).x, her.getPosition(name).y);
			};
		}
	}
}
